<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="任务类型" prop="busstype">
        <el-select v-model="queryParams.busstype" placeholder="请选择任务类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.obj_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions"
        :show-count="true" placeholder="管理部门"
          style="width: 160px" size="small"
          />
      </el-form-item>
      <el-form-item label="执行人" prop="oname">
        <el-select v-model="queryParams.userId" placeholder="选择用户">
          <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName" :value="item.userId"
            :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>



      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.worklog_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sysWorklogList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="index" width="55" align="center" />
      <!-- <el-table-column label="日志id" align="center" prop="logid" v-if="true"/> -->
      <!-- <el-table-column label="节点id" align="center" prop="nid" /> -->
      <!-- <el-table-column label="对象id" align="center" prop="objid" /> -->
       <el-table-column label="任务" align="center" prop="title" width="180"/>
       <el-table-column label="流程" align="center" prop="workflow" width="150">
         <template slot-scope="scope">
             <span>{{scope.row.workflow}} &gt; {{scope.row.name}}</span>
           </template>
         </el-table-column>
      <!-- <el-table-column label="节点名称" align="center" prop="name" /> -->
      <!-- <el-table-column label="业务模块" align="center" prop="busstype" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.obj_type" :value="scope.row.busstype"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="页面路由" align="center" prop="route" /> -->
      <!-- <el-table-column label="经办id" align="center" prop="operatorid" /> -->
      <!-- <el-table-column label="经办类型" align="center" prop="otype" /> -->
      <el-table-column label="经办人" align="center" prop="oname" />
      <el-table-column label="操作人" align="center" prop="userName" width="120"/>
      <el-table-column label="部门" align="center" prop="deptName" />
      <!-- <el-table-column label="内容" align="center" prop="content" /> -->
     <!-- <el-table-column label="前经办日志id" align="center" prop="prelogid" />
      <el-table-column label="前经办人" align="center" prop="preid" />
      <el-table-column label="前经办节点" align="center" prop="prenid" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.worklog_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="结果" align="center" prop="action">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.action_type" :value="scope.row.action"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="memo" /> -->

      <!-- <el-table-column label="关联业务流程" align="center" prop="pid" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['system:sysWorklog:list']">详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sysWorklog:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listSysWorklog, getSysWorklog, delSysWorklog, addSysWorklog, updateSysWorklog } from "@/api/system/sysWorklog";
import {
    treeselect
  } from "@/api/system/dept";
  import {
    getUserSelect
  } from "@/api/system/user";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SysWorklog",
  dicts: ['obj_type', 'node_status','action_type','worklog_status'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程日志表格数据
      sysWorklogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wid: undefined,
        nid: undefined,
        objid: undefined,
        name: undefined,
        busstype: undefined,
        operatorid: undefined,
        otype: undefined,
        oname: undefined,
        userId: undefined,
        deptId: undefined,
        status: undefined,
        prelogid: undefined,
        preid: undefined,
        prenid: undefined,
        action: undefined,
        pid: undefined
      },
      // 表单参数
      form: {},
      deptOptions: [],
      // 用户选项
      userOptions: [],
      // 表单校验
      rules: {
      }
    };
  },
  watch:{
    'queryParams.deptId':{
       handler(newVal, oldVal) {
        this.deptChange();
       },
       deep: true,
       immediate: true
       }
  },
  created() {
    const status = this.$route.query.status;
    this.queryParams.status = status;    
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询流程日志列表 */
    getList() {
      this.loading = true;
      listSysWorklog(this.queryParams).then(response => {
        this.sysWorklogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    listUser() {
      getUserSelect(Object({
        deptId: this.queryParams.deptId
      })).then(response => {
        this.userOptions = response.data;
      });
    },
    deptChange(){
      this.listUser();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logid: undefined,
        wid: undefined,
        nid: undefined,
        objid: undefined,
        name: undefined,
        busstype: undefined,
        route: undefined,
        operatorid: undefined,
        otype: undefined,
        oname: undefined,
        userId: undefined,
        deptId: undefined,
        title: undefined,
        content: undefined,
        status: "0",
        prelogid: undefined,
        preid: undefined,
        prenid: undefined,
        action: undefined,
        memo: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        pid: undefined
      };
      this.resetForm("form");
    },
    handleDetail(row){
      const id = row.objid;
      const logid = row.logid;
      this.$router.push({path:'/system/sysModel/detailLog/' + logid,query:{url:"/workflow/sysWorklog",t: Date.now()}});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const logid = row.logid || this.ids
      getSysWorklog(logid).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改流程日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.logid != null) {
            updateSysWorklog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysWorklog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logids = row.logid || this.ids;
      this.$modal.confirm('是否确认删除流程日志编号为"' + logids + '"的数据项？').then(() => {
        this.loading = true;
        return delSysWorklog(logids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysWorklog/export', {
        ...this.queryParams
      }, `sysWorklog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
